<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;


class BuildingRoomModel extends Model
{

    
    use SoftDelete;
    

    // 表名
    protected $name = 'building_room';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'update_time_text'
    ];

    protected static function init()
    {

        self::beforeWrite(function ($row) {
            if (isset($row['status']) && $row['status'] == 3) {
                HouseModel::where('room_id', $row['id'])
                    ->where('is_finish', 0)
                    ->update(['is_finish' => 1]);
            }
        });
    }


    public static function syncHouseData($room_id, $house = null)
    {
        if (!$house) {
            $house = HouseModel::where([
                'room_id' => $room_id,
                //'is_finish' => 0
            ])
                ->order('id', 'desc')
                ->find();
        }

        if ($house) {
            $water_rate = BuildingRoomBoardModel::where('room_id', $house->room_id)
                ->where('type', 1)->value('price', 0);
            $power_rate = BuildingRoomBoardModel::where('room_id', $house->room_id)
                ->where('type', 2)->value('price', 0);
            $manage_rate = BuildingRoomFixedModel::where('room_id', $house->room_id)
                ->where('type_name', '管理费')->value('price', 0);


            $house->water_rate = $water_rate;
            $house->power_rate = $power_rate;
            $house->manage_rate = $manage_rate;
            $house->save();

        }

    }


    public static function getRoomOwner($room_id)
    {
        $build_id = self::where('id', $room_id)->value('build_id', 0);
        return BuildingModel::where('id', $build_id)->value('user_id', 0);
    }


    public function getFacilitiesList($room_id)
    {
        $option_ids = BuildingRoomPtModel::where('room_id', $room_id)->column('option_id');
        if (empty($option_ids)) {
            return [];
        }
        return (new ConfigValueModel())->where('cid', ConfigValueModel::TYPE_FACILITIES)
            ->whereIn('option_id', $option_ids)
            ->field('option_id,option_name,icon')
            ->select();

    }

    public function building()
    {
        return $this->belongsTo('app\admin\model\BuildingModel', 'build_id', 'id');
    }

    public function board()
    {
        return $this->hasMany('app\admin\model\BuildingRoomBoardModel', 'room_id', 'id');
    }

    public function fixed()
    {
        return $this->hasMany('app\admin\model\BuildingRoomFixedModel', 'room_id', 'id');
    }

    public function pt()
    {
        return $this->hasMany('app\admin\model\BuildingRoomPtModel', 'room_id', 'id');
    }

    public function delUser()
    {
        return $this->belongsTo(\app\common\model\User::class, 'del_uid', 'id')->bind('username');
    }
    public function getStatusList()
    {
        return [0 => '空置', 1 => '招租中', 2 => '待签约', 3 => '已签约'];
    }

    public function getTypeList()
    {
        return [
            1 => '居住',
            2 => '商铺',
            3 => '办公',
            4 => '厂房',
            5 => '仓库',
            6 => '建材',
            7 => '物业',
            8 => '车位',
        ];
    }

    public function getImagesAttr($value, $data)
    {
        $value = $value ?: ($data['images'] ?? []);
        $images = $value ? json_decode($value, true) : [];
        foreach ($images as $k =>$v) {
            $images[$k] = cdnurl($v);
        }
        return $images;
    }

    public function getCoverAttr($value, $data)
    {
        if ($value) {
            return cdnurl($value);
        }
        $value = $this->getImagesAttr('', $data);
        return $value ? $value[0] : '';
    }

    public function getVideoAttr($value, $data)
    {
        $value = $value ?: ($data['video'] ?? '');
        return $value ? cdnurl($value) : '';
    }

    public function getTagsTextAttr($value, $data)
    {
        $tags = $value ?: ($data['tags'] ?? []);
        return ConfigValueModel::getTagsText($tags);
    }

    public function setTagsAttr($value)
    {
        if (is_array($value)) {
            $value = implode(',', $value);
        }
        return $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['type'] ?? '');
        $list = $this->getTypeList();
        return $list[$value] ?? '';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['delete_time'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

}
